//index.js

const app = getApp()

Page({
  data: {
    //轮播图的参数
    autoplay: true,
    interval: 3000,
    duration: 1000,
    swiperCurrent: 0,   //轮播图的点
    types: [],//类型导航条的参数
    activeTypeId: 0,
    banners: []
  },
  //类型导航条的函数
  tabClick: function (e) {
    this.setData({
      activeTypeId: e.currentTarget.typeId
    });

    /*/触发导航条会发生的事件
    this.getGoodsList(this.data.activeTypeId);*/
  },

  onLoad: function () {
    var that = this
    wx.request({
      url: 'http://111.230.44.126:88/api/v1/banner/1',    //轮播图的接口
      data: '',
      header: {},
      method: 'GET',
      dataType: 'json',
      responseType: 'text', 
      header: {
        "content-type": "application/json",
        "token": wx.getStorageSync("token")
      },
      success: function (res) {
        that.setData({
          banners: res.data.items
        })
        console.log(res.data.items);
      },
      fail: function (res) { 
        console.log(res);
      },
      complete: function (res) { },

    }),
      //类别的接口
      wx.request({
        url: 'http://111.230.44.126:88/api/v1/category/all',
        header: {
          "content-type": "application/json",
          "token": wx.getStorageSync("token")
        },
        success: function (res) {
          var types = [{ id: 0, name: "全部" }];
          for (var i = 0; i < res.data.length; i++) {
            types.push(res.data[i]);
          }
          that.setData({
            types: types,
            activeTypeId: 0
          })
          that.getGoodsList(0);
        },
        fail: function (res) { 
          console.log(res);
        },
        complete: function (res) { },
      })
    //商品的接口
    wx.request({
      url: 'http://111.230.44.126:88/api/v1/product/recent',
      data: '',
      method: 'GET',
      dataType: 'json',
      responseType: 'text',
      header: {
        "content-type": "application/json",
        "token": wx.getStorageSync("token")
      },
      success: function (res) {
       
        that.setData({
          goods: res.data,
          activeTypeId: 0
        })
        console.log(res.data);
      },
      fail: function (res) {
        console.log(res);
       },
      complete: function (res) { },
    })

  },
  //滚动条结束后触发这个函数
  swiperchange: function (e) {
    //console.log(e.detail.current)
    this.setData({
      swiperCurrent: e.detail.current
    })
  },
  toDetailsTap: function (e) {
    //获取当前鼠标点击后得到的goodsId
    var goodsid = e.currentTarget.dataset.goodsid;
    console.log(e);
    wx.navigateTo({
      url: '../goods_detail/goods_detail?goodsid=' + goodsid
    })
  },
  //滚动图图片详情的显示
  banner_goods:function(e){
    var goodsid = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '../goods_detail/goods_detail?goodsid=' + goodsid
    })
  },
  //导航分类响应
  tabClick: function (e) {
    var activeTypeId = e.currentTarget.id
    this.setData({
      activeTypeId: activeTypeId
    })
    this.getGoodsList(activeTypeId);
  },
  //获得分类商品
  getGoodsList: function (activeTypeId) {
    if (activeTypeId == 0) {
      this.Type_all();
    }
    else {
      var that = this;
      wx.request({
        url: 'http://111.230.44.126:88/api/v1/product/by_category',
        data: {
          id: activeTypeId
        },
        header: {
          "content-type": "application/json",
          "token": wx.getStorageSync("token")
        },
        success: function (res) {
         
          that.setData({
            goods: res.data
          })
        }
      })
    }
  },
  //分类中的全部
  Type_all: function () {
    var that = this;
    wx.request({
      url: 'http://111.230.44.126:88/api/v1/product/recent',
      header: {
        "content-type": "application/json",
        "token": wx.getStorageSync("token")
      },
      success: function (res) {
       
        that.setData({
          goods: res.data,
        })
        console.log(res.data);
      },
      fail: function (res) {
        console.log(res);
      },
      complete: function (res) { },
    })
  },
  /*下拉刷新页面*/
  onPullDownRefresh: function () {
    wx.showNavigationBarLoading(); //在标题栏中显示加载
    var that = this;
    setTimeout(function () {
      // complete
      wx.hideNavigationBarLoading() //完成停止加载
      wx.stopPullDownRefresh() //停止下拉刷新
      that.setData({
        activeTypeId: 0
      })
      that.getGoodsList(0);
    }, 1500);
  }
})
